
/**
 * Grid 组件
 * 
 */
define(function(require, exports, module) {

			require("common/grid/src/theme/default/css/Grid.css");
			var Math = require("tools/math.uuid/dist/math.uuid");
			require("tools/jquery.pagination")($);
			var GridFormat = require("common/grid/src/js/GridFormat");
			

			var Grid = function(options) {
				this.option = options;

				var columnModel = this.option.columnModel;// 列模型
				var url = this.option.url;// 数据服务
				var renderTo = this.option.renderTo;// 锚点
				var initHead = this.option.initHead;// 初始化表头
				var initBody = this.option.initBody;// 初始化表体

				var afterGeneBody = this.option.afterGeneBody;

				var pageSize = 10;// 页数
				var newID = "";
				var oldID = "";
				var current_page = 1;
				var LEFT = "LEFT";
				var RIGHT = "RIGHT";
				var CENTER = "CENTER";

				/**
				 * ajax获取数据
				 */
				var loadData = function(callback, params) {
					clearBody();
					Status.loading();
					params = "oldSearchId=" + oldID + "&newSearchId=" + newID
							+ "&pageInfo.curPageNo=" + current_page
							+ "&pageInfo.pageSize=" + pageSize + "&" + params;
					$.ajax({
								type : "POST",
								dataType : 'json',
								url : url,
								data : "",
								success : function(result) {
									Status.clear();
									callback(result);
								},
								error:function(result){
									Status.clear();
									callback(result);
								}
							});
				};
				/**
				 * 翻页
				 */
				var page = function() {
					oldID = newID;
					loadData(refreshTable);
				};
				/**
				 * 刷新
				 */
				var refresh = function() {
					loadData(refreshTable);
				};
				/**
				 * 查询数据
				 */
				var queryData = function(params) {
					oldID = newID;
					newID = Math.uuid();
					loadData(refreshGrid, params);
				};

				/**
				 * 初始化grid体
				 */
				var geneHead = function() {
					var cm = columnModel;
					var target = $("#" + renderTo);
					var grid = $("<div>");
					grid.addClass("grid");
					var head = $("<div>").addClass("grid-head");
					var body = $("<div>").addClass("grid-body").attr("id",
							"gridBody");
					if (!initHead) {
						var table = $("<table>");
						var thead = $("<thead>");
						// 生成表头
						var clen = cm.length;
						var thtr = $("<tr>");
						for (var i = 0; i < clen; i++) {
							var th = $("<td>");
							th.append(cm[i].header);
							thtr.append(th);
						}
						thead.append(thtr);
						table.append(thead);
						// 生成表体
						var tbody = $("<tbody>");
						table.append(tbody);
						head = table;
						grid.append(head);
					} else {
						head = initHead();
						grid.append(head);
						grid.append(body);
					}
					// 状态
					var status = $("<div>").attr("id","gridStatus").addClass("grid_status");
					grid.append(status);
					// 分页
					var page = $("<div>").attr("id", "page")
							.addClass("pagination");
					grid.append(page);
					target.append(grid);
				};

				/**
				 * 刷新表格数据
				 */
				var geneBody = function(records) {
					var cm = columnModel;
					var clen = cm.length;
					if (!initBody) {
						var tbody = $(".grid").find("tbody");
						if (records != null) {
							var slen = records.length;
							for (var i = 0; i < slen; i++) {
								var tr = $("<tr>");
								if (i % 2 == 1) {
									tr.addClass("row_even");
								}
								for (var j = 0; j < clen; j++) {
									var td = $("<td>");
									var data = records[i];
									var colId = cm[j].dataIndex;
									var colformat = GridFormat[cm[j].format];
									var align = cm[j].align;
									if (typeof(colformat) == "function") {
										td.append(colformat(data[colId], data));
									} else if (colId != null) {
										td.append(data[colId]);
									}
									// 设置td对齐方式
									if (CENTER == align) {
										td.addClass("grid_td_align_center");
									} else if (RIGHT == align) {
										td.addClass("grid_td_align_right");
									} else {
										td.addClass("grid_td_align_left");
									}
									tr.append(td);
								}
								tbody.append(tr);
							}
						} else {
							Status.noData();
						}
					} else {
						var body = $("#gridBody");
						if (records != null) {
							body.append(initBody(records));
						} else {
							Status.noData();
						}
					}
					/**
					 * 表格创建后扩展点
					 */
					if (afterGeneBody) {
						afterGeneBody();
					}
				};

				/**
				 * 分页刷新grid数据
				 */
				var refreshTable = function(result) {
					geneBody(result.current_data);
				};
				/**
				 * 查询后刷新整个grid信息
				 */
				var refreshGrid = function(result) {
					geneBody(result.current_data);
					initPageBar(result.totalRecords);
				};
				/**
				 * 初始化分页信息
				 */
				var initPageBar = function(total) {
					$("#page").html("");
					if (total != 0) {
						$("#page").pagination(total, {
									'items_per_page' : pageSize,
									'num_display_entries' : 4,
									'num_edge_entries' : 1,
									'callback' : function(pageIndex, jq) {
										current_page = pageIndex + 1;
										page();
										return false;
									}
								});
					}else{
						$("#page").html("");
					}
				};
				/**
				 * grid状态
				 */
				var Status = {
					noData :function(){
						$("#gridStatus").html("没有查到数据");
					},
					loading:function(){
						$("#gridStatus").html("正在加载数据...");
					},
					clear:function(){
						$("#gridStatus").html("");
					}
				};
				/**
				 * 清空grid
				 */
				var clearBody = function(){
					$("#gridBody").html("");	
					$(".grid").find("tbody").html("");
				};
				
				geneHead();
				this.refresh = refresh;

				this.query = function(params) {
					queryData(params);
				};
			};
			module.exports = Grid;
		});